package com.beiing.tianshuai.tianshuai.mine.model;

import com.beiing.tianshuai.tianshuai.entity.LoginBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;
import com.netease.nim.uikit.common.util.log.LogUtil;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Jinni on 2017/7/14 0014.
 */

public class LoginModel implements LoginModelImpl {

    private static final String TAG = "LoginModel";
    private OnLoginListener mOnLoginListener;

    public LoginModel(OnLoginListener onLoginListener) {
        mOnLoginListener = onLoginListener;
    }

    @Override
    public void getLoginResult(String email, String pass) {
        LogUtil.i(TAG,"密码是否加密"+pass);
        //此处不加密，在输入时加密的。因为有一个QQ登录绑定接收后台密码直接登录，不能二次加密
        Observable<LoginBean> loginResult = DataManager.getLoginResult(
                email,
                pass
        );
        Observable<LoginBean> signUpBeanObservable = loginResult.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<LoginBean>() {

            @Override
            public void onNext(@NonNull LoginBean loginBean) {
                if (mOnLoginListener != null) {
                    mOnLoginListener.onSuccess(loginBean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mOnLoginListener != null) {
                    mOnLoginListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    public interface OnLoginListener {

        void onSuccess(LoginBean loginBean);

        void onError(Throwable throwable);
    }
}
